package com.ilop.sthome.ui.activity.scene;

import androidx.recyclerview.widget.GridLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.InputFilterUtil;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.common.command.SiterCommand;
import com.ilop.sthome.data.event.EventAnswerOK;
import com.ilop.sthome.mvp.contract.scene.AddSceneContract;
import com.ilop.sthome.mvp.present.scene.AddScenePresenter;
import com.ilop.sthome.ui.adapter.scene.ChoseColorAdapter;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityAddSceneBinding;

/**
 * @author skygge
 * @date 2020-01-12.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：添加场景页面
 */

public class AddSceneActivity extends ContainLoginActivity<AddScenePresenter, ActivityAddSceneBinding> implements AddSceneContract.IView {

    private int mGateway = -1;
    private ChoseColorAdapter mColorAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_scene;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mGateway = getIntent().getIntExtra(CommonId.KEY_POSITION, 0);
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new AddScenePresenter(mContext);
    }

    @Override
    protected void initView() {
        super.initView();
        InputFilterUtil.setEditTextInhibitInputSpaChat(mContext, mDBind.etSceneName);
        mColorAdapter = new ChoseColorAdapter(mContext);
        mDBind.rvColorList.setLayoutManager(new GridLayoutManager(mContext, 5));
        mDBind.rvColorList.setAdapter(mColorAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        mColorAdapter.setColor(-1);
        LiveDataBus.get().with(CommonId.CHOOSE_SCENE_COLOR, Integer.class).observe(this, integer -> {
            mColorAdapter.setColor(integer);
            mPresent.getDefaultColorCode(String.valueOf(integer));
        });
     }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivSceneBack.setOnClickListener(view -> {
            String mSceneName = mDBind.etSceneName.getText().toString();
            if (!TextUtils.isEmpty(mSceneName)){
                mPresent.whetherToSave(mSceneName, mGateway);
            }else {
                finish();
            }
        });
        mDBind.tvAddScene.setOnClickListener(view -> {
            String mSceneName = mDBind.etSceneName.getText().toString();
            if (TextUtils.isEmpty(mSceneName)){
                showToastMsg(getString(R.string.input_scene_name));
            }else {
                showProgressDialog();
                mPresent.onSendScene(mSceneName, mGateway);
            }
        });
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.KEY_ANSWER_OK, EventAnswerOK.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "initDataBus: answerOK");
                dismissProgressDialog();
                if(event.getData_str1().length()==9) {
                    int cmd = Integer.parseInt(event.getData_str1().substring(0, 4), 16);
                    if (cmd == SiterCommand.INCREASE_SCENE) {
                        if (CommonId.KEY_SUCCESS.equals(event.getData_str2())) {
                            mPresent.onSaveScene();
                            finish();
                        }else {
                            showToast(getString(R.string.failed));
                        }
                    }
                }else {
                    showToast(getString(R.string.failed));
                }
            }
        });
    }

    @Override
    public void showToastMsg(String msg) {
        dismissProgressDialog();
        showToast(msg);
    }


    @Override
    public void finishActivity() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            String mSceneName = mDBind.etSceneName.getText().toString();
            if (!TextUtils.isEmpty(mSceneName)){
                mPresent.whetherToSave(mSceneName, mGateway);
            }else {
                finish();
            }
            return true;
        }else {
            return super.onKeyDown(keyCode, event);
        }
    }
}
